package com.avic.rong.adapter.collect;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.avic.rong.R;
import com.avic.rong.activity.mine.CollectionActivity;
import com.avic.rong.activity.product.DetailProductActivity;
import com.avic.rong.bean.collect.GoodsCollectBean;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.userOrder.api.AddCarApi;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.DeviceUtils;
import com.avic.rong.utils.ToastTools;
import com.avic.rong.wedgit.StringUtil;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GoodsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;

    private ArrayList<GoodsCollectBean> mList;


    public GoodsAdapter(Context mContext, ArrayList<GoodsCollectBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    private int TYPE_ONE = 1;
    private int TYPE_TWO = 2;

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_goods_two, viewGroup, false);
            return new ShopHolder_Two(inflate);
    }
    public static String stripHtml(String content) {
        // <p>段落替换为换行
        content = content.replaceAll("<p .*?>", "\r\n");
        // <br><br/>替换为换行
        content = content.replaceAll("<br\\s*/?>", "\r\n");
        // 去掉其它的<>之间的东西
        content = content.replaceAll("\\<.*?>", "");
        // 还原HTML
        // content = HTMLDecoder.decode(content);
        //&ldquo;&quot;&nbsp;
        content = content.replaceAll("&.dquo;", "\"");
        content = content.replaceAll("&nbsp;", " ");
        return content;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {

        final GoodsCollectBean bean = mList.get(i);
        ShopHolder_Two shopHolderTwo = (ShopHolder_Two) viewHolder;
        shopHolderTwo.tv_book_name.setText(bean.getGoodsName());
        DecimalFormat df = new DecimalFormat("#####0.00");
        shopHolderTwo.tv_book_price.setText("¥"+df.format(bean.getGoodsPrice()));
        if (!StringUtil.isEmpty(bean.getGoodsIntro())) {
            shopHolderTwo.tv_tv_press.setText(stripHtml(bean.getGoodsIntro()));
        }
        String keywords = bean.getKeywords();
        String catName = bean.getCatName();
        int goodsType = bean.getGoodsType();
        if (goodsType>=40&&goodsType<=44){
            shopHolderTwo.llNopicture.setVisibility(View.VISIBLE);
            shopHolderTwo.tvCategory.setText(catName);
            shopHolderTwo.tvNo.setText(keywords);
            shopHolderTwo.img_goods.setVisibility(View.GONE);
        }else {
            shopHolderTwo.llNopicture.setVisibility(View.GONE);
            shopHolderTwo.img_goods.setVisibility(View.VISIBLE);
            Glide.with(mContext).applyDefaultRequestOptions(RequestOptions.errorOf(R.mipmap.doc_default)).load(bean.getGoodsImg()).into(shopHolderTwo.img_goods);
        }

        //侧滑删除
        shopHolderTwo.btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (buttonClick!=null){
                    buttonClick.onClick(bean,i);
                }
            }
        });

        /*//添加购物车
        shopHolderTwo.img_shop_car.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String skuStr = "{"+"\""+bean.getTargetId()+"\""+":"+"1}";
                addCar(skuStr);
            }
        });*/


        //点击条目详情
        shopHolderTwo.llGoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mContext.startActivity(DetailProductActivity.getIntent(mContext,bean.getTargetId()+""));
            }
        });
    }

    //添加购物车
    private void addCar(String skuStr) {
        Log.e("cjx", "skuStr:" + skuStr);
        String login_token = SharedPreferencesUtil.getInstance(mContext).getLogintoken();
        if (!login_token.equals("")) {
            Map<String, String> map = new HashMap<>();
            map.put("skuStr", skuStr);
            map.put("orderType", 4 + "");
//            是否再次购买（0/1）
            map.put("buyAgain", 0 + "");
            map.put("token",login_token);
            map.put("deviceId", DeviceUtils.getIMEString(mContext));
            final AddCarApi addCarApi = new AddCarApi((CollectionActivity)mContext, simpleOnNextListenerAddCar);
            addCarApi.setCache(false);
            addCarApi.setMap(map);
            HttpManager.getInstance().doHttpDeal(addCarApi);
        }else{
            Toast.makeText(mContext,"token为空", Toast.LENGTH_LONG).show();
        }
    }

    //添加购物车
    HttpOnNextListener simpleOnNextListenerAddCar = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)){
                ToastTools.showToast(mContext,subjects.getMsg());
                return;
            }
            ToastTools.showToast(mContext, "添加成功");
        }

        @Override
        public void onCacheNext(String cache) {

        }

        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", e.getMessage());
        }
    };



    @Override
    public int getItemCount() {
        return mList.size();
    }

  /*  @Override
    public int getItemViewType(int position) {
        if (position%2==0){
            return TYPE_ONE;
        }
        return TYPE_TWO;
    }*/

  /*  class ShopHolder_One extends RecyclerView.ViewHolder {
        @BindView(R.id.img_goods)
        ImageView img_goods;
        @BindView(R.id.img_shop_car)
        ImageView img_shop_car;
        @BindView(R.id.tv_book_name)
        TextView tv_book_name;
        @BindView(R.id.tv_tv_press)
        TextView tv_tv_press;
        @BindView(R.id.tv_author)
        TextView tv_author;
        @BindView(R.id.tv_book_price)
        TextView tv_book_price;
        public ShopHolder_One(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }*/
    class ShopHolder_Two extends RecyclerView.ViewHolder {
        @BindView(R.id.img_goods)
        ImageView img_goods;
        /*@BindView(R.id.img_shop_car)
        ImageView img_shop_car;*/
        @BindView(R.id.tv_book_name)
        TextView tv_book_name;
        @BindView(R.id.tv_tv_press)
        TextView tv_tv_press;
        @BindView(R.id.tv_book_price)
        TextView tv_book_price;
        @BindView(R.id.collect_btn_delete)
        Button btn_delete;
        @BindView(R.id.ll_goods)
        LinearLayout llGoods;
        @BindView(R.id.ll_no_picture)
        LinearLayout llNopicture;
        @BindView(R.id.tv_category)
        TextView tvCategory;
        @BindView(R.id.tv_no)
        TextView tvNo;
        public ShopHolder_Two(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
    private ButtonClick buttonClick;

    public void setButtonClick(ButtonClick buttonClick) {
        this.buttonClick = buttonClick;
    }

    public interface ButtonClick{
        void onClick(GoodsCollectBean collectBean,int position);
    }

}
